import Vue from 'vue';
import {
    url,
} from '@/lib/utils';
import {
    api,
} from '@/lib/api';

import { Module } from 'vuex';
import { RootState } from '@/types/store';

export interface ProductState {
    productList: {
        [sort_screen: string]: ListWrap<{ id: number; type: Product.Type }> & ListReq;
    };
    productMap: {
        [productId: number]: Product.ShopListItem;
    };
}

const m: Module<ProductState, RootState> = {
    namespaced: true,
    state: {
        productList: {},
        productMap: {},
    },
    mutations: {
        /**
         * 设置普通商品列表
         */
        initProductList(state, key) {
            Vue.set(state.productList, key, {
                list: [],
                req: false,
                loaded: false,
            });
        },
        setProductListReq(state, { key, req }) {
            state.productList[key].req = req;
        },
        setProductListLoaded(state, { key, loaded }) {
            state.productList[key].loaded = loaded;
        },
        concatProductList(state, { key, list }) {
            const idList = state.productList[key].list;
            list.forEach((item) => {
                idList.push({
                    type: item.type,
                    id: item.id,
                });
                Vue.set(state.productMap, item.type + '_' + item.id, item);
            });
            Vue.set(state.productList[key], 'list', idList);
        },
    },
    actions: {
        /**
         * 获取普通类商品列表
         */
        async getProductList({ state, commit }, { offset, count, sort = null, screen = null, forceUpdate = false }) {
            const key = `${sort}_${JSON.stringify(screen)}`;
            // console.log(key);
            if (forceUpdate || !state.productList[key]) {
                commit('initProductList', key);
            }
            const wrap = state.productList[key];
            const length = wrap.list.length;
            const end = offset + count;
            if (wrap.loaded || wrap.req) {
                return;
            }
            if (end <= length) {
                return;
            }
            if (offset < length) {
                offset = length;
                count = end - length;
            }
            commit('setProductListReq', { key, req: true });
            const res = await api.post({
                url: `${url}/api/shop/goods`,
                data: {
                    offset,
                    count,
                    sort: sort,
                    screen: screen,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('concatProductList', {
                    key,
                    list,
                });
                if (list.length < count) {
                    commit('setProductListLoaded', {
                        key,
                        loaded: true,
                    });
                }
            }
            commit('setProductListReq', { key, req: false });
            return res;
        },
    },
};

export default m;
